﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AppCode_Admin_UpdateCompany : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["userNameAdmin"] == null)
            {
                Response.Redirect("..\\Home\\HomePage.aspx");
            }
        }
        Label5.Visible = false;
        Label6.Visible = false;
        Label7.Visible = false;
        Label8.Visible = false;
        Label9.Visible = false;
        if (!IsPostBack)
        {
            String id = Request.QueryString["id"];
            String connect = WebConfigurationManager.ConnectionStrings["ConnectDatabase"].ToString();
            SqlConnection sqlConnect = new SqlConnection(connect);
            sqlConnect.Open();
            SqlCommand sqlCommand = new SqlCommand("Select *From CompanyDetails where CompanyId=" + id, sqlConnect);
            SqlDataReader reader = sqlCommand.ExecuteReader();
            reader.Read();
            txtName.Text = reader.GetString(1);
            txtAddress.Text = reader.GetString(2);
            txtPhone.Text = reader.GetString(3);
            txtUrl.Text = reader.GetString(4);
            sqlConnect.Close();
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Boolean check = false;
        if (txtName.Text.Equals(""))
        {
            Label5.Visible = true;
            check = true;
        }
        if (txtAddress.Text.Equals(""))
        {
            Label6.Visible = true;
            check = true;
        }
        if (txtPhone.Text.Equals(""))
        {
            Label7.Visible = true;
            check = true;
        }
        if (txtUrl.Text.Equals(""))
        {
            Label8.Visible = true;
            check = true;
        } if (!check)
        {
            String id = Request.QueryString["id"];
            String connect = WebConfigurationManager.ConnectionStrings["ConnectDatabase"].ToString();
            SqlConnection sqlConnect = new SqlConnection(connect);
            sqlConnect.Open();
            SqlCommand sqlCommand = new SqlCommand("Update CompanyDetails Set Address=@Address,Phone=@Phone,CompanyUrl=@CompanyUrl where CompanyId=" + id, sqlConnect);
            sqlCommand.Parameters.AddWithValue("@Address", txtAddress.Text);
            sqlCommand.Parameters.AddWithValue("@Phone", txtPhone.Text);
            sqlCommand.Parameters.AddWithValue("@CompanyUrl", txtUrl.Text);
            sqlCommand.ExecuteNonQuery();
            sqlConnect.Close();
            Response.Redirect("CompanyDetails.aspx");
        }
    }

}